var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/_utils/hooks/useOther.ts
var useOther_exports = {};
__export(useOther_exports, {
  default: () => useOther
});
module.exports = __toCommonJS(useOther_exports);
var import_react = require("react");
var import_hooks = require("@szzj/hooks");
function useOther({
  mode = "multiple",
  options,
  value,
  onChange
}) {
  const [selectedValue, setSelectedValue] = (0, import_react.useState)([]);
  const [otherValue, setOtherValue] = (0, import_react.useState)(void 0);
  const [otherChecked, setOtherChecked] = (0, import_react.useState)(false);
  const optionsRef = (0, import_hooks.useRealtimeRef)(options);
  (0, import_react.useEffect)(() => {
    if (value) {
      const optionValues = optionsRef.current.map(
        (option) => option.value
      );
      const newSelectedValue = [];
      let otherVal;
      (Array.isArray(value) ? value : [value]).forEach((val) => {
        if (optionValues.includes(val)) {
          newSelectedValue.push(val);
        } else {
          otherVal = val;
        }
      });
      setSelectedValue(newSelectedValue);
      setOtherValue(otherVal);
      if (otherVal !== void 0)
        setOtherChecked(true);
    }
  }, [value]);
  const changeSelectedValue = (0, import_react.useCallback)(
    (vals) => {
      if (Array.isArray(vals)) {
        setSelectedValue(vals);
        if (otherValue !== void 0) {
          onChange == null ? void 0 : onChange([
            ...vals,
            otherValue
          ]);
        } else {
          onChange == null ? void 0 : onChange([...vals]);
        }
      } else {
        setSelectedValue([vals]);
        onChange == null ? void 0 : onChange(vals);
        setOtherValue(void 0);
        setOtherChecked(false);
      }
    },
    [otherValue, onChange]
  );
  const changeOtherValue = (0, import_react.useCallback)(
    (val) => {
      setOtherValue(val);
      if (mode === "multiple") {
        onChange == null ? void 0 : onChange(
          val !== void 0 ? [...selectedValue, val] : selectedValue
        );
      } else {
        onChange == null ? void 0 : onChange(val);
      }
    },
    [selectedValue, mode, onChange]
  );
  const changeOtherChecked = (0, import_react.useCallback)(
    (checked) => {
      setOtherChecked(checked);
      if (!checked) {
        changeOtherValue();
      } else {
        if (mode !== "multiple")
          setSelectedValue([]);
      }
    },
    [changeOtherValue]
  );
  return {
    selectedValue,
    otherValue,
    otherChecked,
    changeSelectedValue,
    changeOtherValue,
    changeOtherChecked
  };
}
